package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.ddyx.entity.EventClickFavorite;
import com.ddyx.entity.EventClickLike;
import com.ddyx.entity.EventLog;
import com.ddyx.mapper.EventClickFavoriteMapper;
import com.ddyx.mapper.EventLogMapper;
import com.ddyx.service.EventClickFavoriteService;
import com.ddyx.util.EventTag;
import com.ddyx.util.MD5FileUtil;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Slf4j
@Service
public class EventClickFavoriteServiceImpl implements EventClickFavoriteService {
	@Resource
	EventClickFavoriteMapper eventClickFavoriteMapper;
	@Resource
	EventLogMapper eventLogMapper;
	
	/*
	 * 客户新增收藏商品
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm insertEventClickFavorite(RequestForm param, HttpServletRequest req) {
		ResponseForm result = new ResponseForm();
		EventClickFavorite eventClickFavorite = new EventClickFavorite();
		String requestUrl = req.getRequestURL().toString();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			MyBeanUtils.populate(eventClickFavorite, paramMap);
			eventClickFavorite.setEventTime(new Date());
			eventClickFavoriteMapper.insertSelective(eventClickFavorite);
			
			{//新增收藏用户事件同步信息
				EventLog eventLog  = new EventLog();
				eventLog.setEventTag(EventTag.CLICKFAVORITE);
				eventLog.setEventTime(eventClickFavorite.getEventTime());
				eventLog.setRequestUrl(requestUrl);
				eventLog.setStoreNo(eventClickFavorite.getStoreNo());
				eventLog.setUrlMd5(MD5FileUtil.getMD5String(requestUrl));
				eventLog.setWxUid(eventClickFavorite.getWxUid());
				eventLogMapper.insertSelective(eventLog);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	/*
	 * 客户取消收藏商品
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm cancelEventClickFavorite(RequestForm param, HttpServletRequest req) {
		ResponseForm result = new ResponseForm();
		EventClickFavorite eventClickFavorite = new EventClickFavorite();
		String requestUrl = req.getRequestURL().toString();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			MyBeanUtils.populate(eventClickFavorite, paramMap);
			Example example = new Example(EventClickFavorite.class);
			Criteria criteria = example.createCriteria();
			criteria.andEqualTo("wxUid", eventClickFavorite.getWxUid());
			criteria.andEqualTo("goodsId", eventClickFavorite.getGoodsId());
			eventClickFavoriteMapper.deleteByExample(example);
			// eventClickFavoriteMapper.delete(eventClickFavorite);
			
			{// 用户取消收藏事件同步信息 
				EventLog eventLog = new EventLog();
				eventLog.setEventTag(EventTag.CANCELFAVORITE);
				eventLog.setEventTime(new Date());
				eventLog.setRequestUrl(requestUrl);
				eventLog.setStoreNo(eventClickFavorite.getStoreNo());
				eventLog.setUrlMd5(MD5FileUtil.getMD5String(requestUrl));
				eventLog.setWxUid(eventClickFavorite.getWxUid());
				eventLogMapper.insertSelective(eventLog);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	/*
	 * 根据客户查询收藏商品列表
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm findEventClickFavoriteByCustomer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			com.ddyx.util.Page.parse(paramMap);
			Page<EventClickFavorite> page = PageHelper.startPage((int) paramMap.get("page"),
					(int) paramMap.get("size"));
			List<EventClickFavorite> list = eventClickFavoriteMapper.findEventClickFavoriteByCustomer(paramMap);
			result.setData(list);
			result.setTotal((int) page.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	/*
	 * 门店获取收藏客户列表
	 */
	@Override
	public ResponseForm getFavoriteCustomer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String, Object> paramMap = (Map<String, Object>) param.getData();
		try {
			paramMap = com.ddyx.util.Page.parsePage(paramMap);
			Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
			List<Map<String, Object>> list = eventClickFavoriteMapper.getFavoriteCustomer(paramMap);
			result.setData(list);
			result.setTotal((int) pageHelper.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

}
